<template>
  <el-card shadow="hover">
<!--    条件查询导航-->
    <el-form :inline="true" :model="studentSearch" class="demo-form-inline">
      <el-form-item label="学生姓名">
        <el-input v-model="studentSearch.sname" placeholder="学生姓名"></el-input>
      </el-form-item>
      <el-form-item label="住址">
        <el-input v-model="studentSearch.addr" placeholder="学生住址"></el-input>
      </el-form-item>
      <el-form-item label="所在班级">
        <el-select v-model="studentSearch.cid" placeholder="所在班级">
          <el-option label="所有班级" value="0"></el-option>
          <el-option :label="c.cname" :value="c.cid" v-for="c in classes" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="search" plain>查询</el-button>
        <el-button type="success" plain>添加</el-button>
        <el-button type="danger" plain>批量删除</el-button>
      </el-form-item>
    </el-form>

    <el-table
      :data="students"
      style="width: 100%">
      <el-table-column
        label="学生姓名"
        prop="sname"
        align="center"
        width="180">
      </el-table-column>
      <el-table-column
        label="学生性别"
        prop="sex"
        align="center"
        width="180">
      </el-table-column>
      <el-table-column
        label="年龄"
        prop="age"
        align="center"
        width="180">
      </el-table-column>
      <el-table-column
        label="住址"
        prop="addr"
        align="center"
        width="180">
      </el-table-column>
      <el-table-column
        align="center"
        label="所在班级"
        prop="cname"
        width="180">
      </el-table-column>
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-button
            size="mini"
            @click="handleEdit(scope.$index, scope.row)" type="primary" icon="el-icon-edit" circle></el-button>
          <el-button
            size="mini"
            type="danger"
            @click="handleDelete(scope.$index, scope.row)" icon="el-icon-delete" circle></el-button>
        </template>
      </el-table-column>
    </el-table>

<!--    分页导航-->
    <el-pagination
      align="right"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="page"
      :page-sizes="[5, 10, 15, 20]"
      :page-size="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>
  </el-card>
</template>

<script>

  import {findStudents,findByPage,search} from '@/api/student'
  import {findClasses} from '@/api/classes'

  export default {
    data() {
      return {
        classes:[],     //列表班级
        students: [],   //列表学生
        page:1,         //当前页
        pageSize:5,     //每页大小
        total:100,      //总记录数
        studentSearch:{}, //条件查询
      }
    },
    created() {
      //1. 列表所有学生
      // this.findStudents();
      //2. 分页查询
      // this.findByPage()
      //3. 条件查询带分页
      this.search()
      //4. 查询班级
      this.findClasses()
    },
    methods: {
      //1. 列表所有学生
      findStudents(){
        findStudents().then(({data})=>{
          this.students = data.data
        })
      },
      //2. 分页查询
      findByPage(){
        findByPage(this.page,this.pageSize).then(({data})=>{
          //2.1 更新总记录数
          this.total = data.pr.total
          //2.2 更新当前总记录页面的集合
          this.students = data.pr.rows
        })
      },

      //3. 查询班级
      findClasses(){
        findClasses().then(({data})=>{
          this.classes = data.data
        })
      },
      //1.4 开始条件查询带分页
      search(){
        search(this.page,this.pageSize,JSON.stringify(this.studentSearch)).then(({data})=>{
          console.log(data)
          //1.4.1 更新总记录数
          this.total = data.pr.total;
          //1.4.2 更新当前页面的总的记录集合
          this.students = data.pr.rows;
        })
      },

      handleEdit(index, row) {
        console.log(index, row);
      },
      handleDelete(index, row) {
        console.log(index, row);
      },
      //每页大小改变时调用
      handleSizeChange(val) {
        console.log(`每页 ${val} 条`);
        this.pageSize = val
        // this.findByPage()
        this.search()
      },
      //当前页改变时调用
      handleCurrentChange(val) {
        this.page = val
        // this.findByPage()
        this.search()
        console.log(`当前页: ${val}`);
      }
    },
  }
</script>

<style scoped>

  .el-pagination{
    margin-top: 20px;
  }

</style>
